package com.relatev.minecraft.TRMobAPI;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TRMobJsonSerializer implements JsonSerializer<TRMob>{
    
    private static Gson gson = new Gson();

    @Override
    public JsonElement serialize(TRMob tm, Type type, JsonSerializationContext jsc) {
        JsonObject jo = new JsonObject();
        for(Field field:ReflectionUtils.getDeclaredFieldsIncludingParents(tm.getClass())){
            field.setAccessible(true);
            if(field.isAnnotationPresent(TRFieldConfigurable.class)){
                try {
                    jo.add(field.getName(), gson.toJsonTree(field.get(tm)));
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                    Logger.getLogger(TRMobJsonSerializer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return jo;
    }
}
